<?php

/**
 * mForge Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2011, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */


    error_reporting(-1);

    /**
     * Deploys the application by merging class files into a single file. This
     * can dramatically improve performance (depending on how many files that
     * are included normally).
     */

    $target = 'classes.php';

    $paths = array(
        '../lib',
        '../modules'
    );

    require_once '../lib/mForge/Application.php';

    foreach ($paths as $path) {

        $iterator = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($path),
            \RecursiveIteratorIterator::LEAVES_ONLY
        );

        foreach ($iterator as $file) {
            if (strpos($file->getPathName(), DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR)) {
                continue;
            }
            if ($file->isFile()
                && substr_compare($file, '.php', -4) === 0) {
                    echo $file . '<br>';
                    require_once $file->getPathName();
            }
        }
    }

    $targetFileContent = '';

    $interfacesAndClasses = array_merge(get_declared_interfaces(), get_declared_classes());
    foreach ($interfacesAndClasses as $interfaceOrClass) {
        $reflection = new \ReflectionClass($interfaceOrClass);
        $fileName = $reflection->getFileName();
        if (!empty($fileName)) {
            $fileArray = file($fileName);
            $targetFileContent .= 'namespace ' . $reflection->getNamespaceName() . " {\n";
            foreach ($fileArray as $row) {
                //TODO: better regexp
                if (preg_match('/^\s*use\s+.+;\s*$/', $row)) {
                    $targetFileContent .= $row;
                }
            }
            for ($i = ($reflection->getStartLine() - 1); $i < $reflection->getEndLine(); $i++) {
                $targetFileContent .= $fileArray[$i];
            }
            $targetFileContent .= "}\n";
        }
    }
    $targetFileContent = "<?php\n" . $targetFileContent;

    $fileWritten = file_put_contents($target, $targetFileContent);

    if ($fileWritten === false) {
        throw new \Exception('Could not write the target file.');
    }
